// pages/me/login/phone_login.js
let util = require('../../utils/util.js');

var intervalFef;

Page({

  data: {
    lessTime: 0,
    phoneNum: "",
    smsCode: "",
    moreHeight: 0,
  },

  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        let windowHeight = (res.windowHeight * (750 / res.windowWidth)); //将高度由 px转化为rpx
        this.setData({
          moreHeight: windowHeight - 300 + 80
        });
      },
    });
  },

  onUnload: function () {
    if (intervalFef) {
      clearInterval(intervalFef);
    }
  },

  bindInputPhone: function (e) {
    this.setData({
      phoneNum: e.detail.value
    });
  },

  bindInputSmsCode: function (e) {
    this.setData({
      smsCode: e.detail.value
    });
  },


  login_tap: function () {
    console.log("---" + this.data.phoneNum);

    if (!util.checkPhoneNumber(this.data.phoneNum)) return;
    if (!this.data.smsCode && this.data.smsCode.length > 0) {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
      })
      return;
    }
    getApp().$http.login(this.data.phoneNum, this.data.smsCode).then(res => {
      wx.setStorageSync('token', res.token)
      getApp().globalData.token = res.token
      wx.reLaunch({
        url: '/pages/main/index/index',
      })
    })

  },

  getMsgCode: function (e) {

    if (!util.checkPhoneNumber(this.data.phoneNum)) return;

    if (this.data.lessTime > 0) return;

    getApp().$http.getMsgCode(this.data.phoneNum).then(res => {
      wx.showToast({
        title: '验证码发送成功',
        icon: 'none'
      })
      this.data.lessTime = 60;
      intervalFef = setInterval(this.countTime, 1000);
    })
  },

  countTime: function () {
    if (this.data.lessTime == 0) {
      intervalFef = clearInterval(intervalFef);
      return;
    }
    this.data.lessTime--;
    this.setData({
      lessTime: this.data.lessTime
    });
  },




})